using System;
using System.Collections.Generic;
using System.Drawing;
using GTA;
using NativeUI;

namespace CustomPeds.Menus;

public class PropSubMenu : Script
{
    public static UIMenu menu = new UIMenu("", TextSetting.props_Menu, new Point(0, -107));

    private static List<dynamic> drawableList = new List<object>(new string[1] { string.Empty });
    private static List<dynamic> textureList = new List<object>(new string[1] { string.Empty });
    private static List<dynamic> attachList = new List<object>(new string[2] { "False", "True" });
    private static UIMenuListItem drawableItem;
    private static UIMenuListItem textureItem;
    private static UIMenuListItem attachItem;
    private static int pid;

    public PropSubMenu()
    {
        Main.pool.Add(menu);
        menu.OnListChange += OnListChange;
        menu.OnItemSelect += OnItemSelect;
        menu.OnMenuClose += OnMenuClose;
    }

    public static void InitpropSubMenu(int _pid)
    {
        pid = _pid;
        UpdatePropsValue();
        drawableList = new List<object>();
        textureList = new List<object>();
        Skin.Prop prop;
        int[] array;
        if (DressingScript.Dressing == Main.Member.P2)
        {
            prop = Main.P2.skin.Props[pid];
            array = Main.P2.skin.PropsTextureData[pid];
        }
        else
        {
            prop = Main.P1.skin.Props[pid];
            array = Main.P1.skin.PropsTextureData[pid];
        }
        int drawable = prop.drawableId;
        int index = prop.textureId;
        bool attach = prop.attach;
        if (prop.drawableId == -1)
        {
            drawable = 0;
            index = 0;
        }
        menu.Clear();
        for (int i = 0; i < prop.drawableNumber; i++) drawableList.Add(i);
        for (int j = 0; j < array[drawable]; j++) textureList.Add(j);
        drawableItem = new UIMenuListItem(TextSetting.props_ListItem_Drawable, drawableList, drawable);
        textureItem = new UIMenuListItem(TextSetting.props_ListItem_Texture, textureList, index);
        attachItem = new UIMenuListItem(TextSetting.props_ListItem_Attach, attachList, Convert.ToInt32(attach));
        menu.SetBannerType(new UIResRectangle());
        menu.AddItem(drawableItem);
        menu.AddItem(textureItem);
        menu.AddItem(attachItem);
        menu.AddItem(new UIMenuItem(TextSetting.generic_MenuItem_Clear));
        menu.AddItem(new UIMenuItem(TextSetting.generic_MenuItem_Distance));
        menu.AddItem(new UIMenuItem(TextSetting.generic_MenuItem_TurnLeft));
        menu.AddItem(new UIMenuItem(TextSetting.generic_MenuItem_TurnRight));
        menu.AddItem(new UIMenuItem(TextSetting.generic_MenuItem_AngleUp));
        menu.AddItem(new UIMenuItem(TextSetting.generic_MenuItem_AngleDown));
        menu.RefreshIndex();
        menu.Visible = true;
    }

    public static void UpdatePropsValue()
    {
        if (DressingScript.Dressing == Main.Member.P2)
        {
            Main.P2.skin.GetPropsValue();
            Main.P2.skin.GetPropsVariableNumber();
            return;
        }
        Main.P1.skin.GetPropsValue();
        Main.P1.skin.GetPropsVariableNumber();
        Main.Clone.skin.GetPropsValue();
        Main.Clone.skin.GetPropsVariableNumber();
    }

    private static void OnListChange(UIMenu sender, UIMenuListItem list, int index)
    {
        if (DressingScript.Dressing == Main.Member.P2)
        {
            if (list.Text == sender.MenuItems[0].Text)
            {
                SkinHelper.SetPedPropIndex(Main.P2.Character, pid, drawableItem.Index, 0, attach: false);
                InitpropSubMenu(pid);
            }
            if (list.Text == sender.MenuItems[2].Text)
            {
                Main.P2.skin.Props[pid].attach = (index != 0);
            }
            SkinHelper.SetPedPropIndex(Main.P2.Character, pid, drawableItem.Index, textureItem.Index, Convert.ToBoolean(attachList[attachItem.Index]));
            Main.P2.skin.Props[pid].action = true;
            return;
        }
        if (list.Text == sender.MenuItems[0].Text)
        {
            SkinHelper.SetPedPropIndex(Main.P1.Character, pid, drawableItem.Index, 0, attach: false);
            SkinHelper.SetPedPropIndex(Main.Clone.Character, pid, drawableItem.Index, 0, attach: false);
            InitpropSubMenu(pid);
        }
        if (list.Text == sender.MenuItems[2].Text)
        {
            Main.P1.skin.Props[pid].attach = (index != 0);
            Main.Clone.skin.Props[pid].attach = (index != 0);
        }
        SkinHelper.SetPedPropIndex(Main.P1.Character, pid, drawableItem.Index, textureItem.Index, Convert.ToBoolean(attachList[attachItem.Index]));
        SkinHelper.SetPedPropIndex(Main.Clone.Character, pid, drawableItem.Index, textureItem.Index, Convert.ToBoolean(attachList[attachItem.Index]));
        Main.P1.skin.Props[pid].action = true;
        Main.Clone.skin.Props[pid].action = true;
    }

    private static void OnItemSelect(UIMenu sender, UIMenuItem item, int index)
    {
        switch (index)
        {
            case 3:
            {
                drawableItem.Index = 0;
                textureItem.Index = 0;
                attachItem.Index = 0;
                if (DressingScript.Dressing == Main.Member.P2)
                {
                    SkinHelper.ClearPedProp(Main.P2.Character, pid);
                    Main.P2.skin.Props[pid].attach = false;
                    Main.P2.skin.Props[pid].action = false;
                }
                else
                {
                    SkinHelper.ClearPedProp(Main.P1.Character, pid);
                    SkinHelper.ClearPedProp(Main.Clone.Character, pid);
                    Main.P1.skin.Props[pid].attach = false;
                    Main.Clone.skin.Props[pid].attach = false;
                    Main.P1.skin.Props[pid].action = false;
                    Main.Clone.skin.Props[pid].action = false;
                }
                InitpropSubMenu(pid);
                break;
            }
            case 4:
                DressingScript.Distance();
                break;
            case 5:
                DressingScript.TurnLeft();
                break;
            case 6:
                DressingScript.TurnRight();
                break;
            case 7:
                DressingScript.AngleUp();
                break;
            case 8:
                DressingScript.AngleDown();
                break;
        }
    }

    private static void OnMenuClose(UIMenu sender)
    {
        menu.Visible = false;
        menu.RefreshIndex();
        // 退格仅关闭菜单，避免重入
    }
}


